package com.huawei.hwid.b;

import android.content.Context;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Looper;
import android.text.TextUtils;
import com.huawei.hms.framework.common.PackageUtils;
import com.huawei.hms.framework.network.grs.GrsApi;
import com.huawei.hms.framework.network.grs.GrsBaseInfo;
import com.huawei.hms.framework.network.grs.local.model.CountryCodeBean;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.vermanager.VersionManager;

public class a {
    private GrsBaseInfo a;

    a() {
    }

    public String a(Context context, String str, String str2) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            throw new IllegalThreadStateException("must not be called on the UI thread");
        } else if (str == null || str2 == null) {
            throw new IllegalArgumentException("service, key must not be null");
        } else {
            a(context, "CN");
            HwAccount b = com.huawei.hwid.d.a.a(context).b();
            if (!(b == null || TextUtils.isEmpty(b.a()))) {
                this.a.setSerCountry(b.a());
            }
            return a(context, this.a, str, str2);
        }
    }

    private static String a(Context context, GrsBaseInfo grsBaseInfo, String str, String str2) {
        e.b("GRSApiImpl", "Query GRS service: " + str + ", key: " + str2 + "params: " + a(grsBaseInfo), true);
        if (Looper.myLooper() == Looper.getMainLooper()) {
            throw new IllegalThreadStateException("must not be called on the UI thread");
        }
        if (TextUtils.isEmpty(str)) {
            str = "com.huawei.cloud.hwid";
        }
        return GrsApi.synGetGrsUrl(str, "Root");
    }

    private void a(Context context, String str) {
        if (this.a == null) {
            CountryCodeBean countryCode = GrsApi.getCountryCode(context, false);
            String countryCode2 = countryCode.getCountryCode();
            String countrySource = countryCode.getCountrySource();
            this.a = new GrsBaseInfo();
            this.a.setAppName(VersionManager.b().c());
            GrsBaseInfo grsBaseInfo = this.a;
            if (TextUtils.isEmpty(str)) {
                str = countryCode2;
            }
            grsBaseInfo.setSerCountry(str);
            this.a.setCountrySource(countrySource);
            this.a.setAndroidVersion(VERSION.RELEASE);
            this.a.setDeviceModel(Build.MODEL);
            this.a.setRomVersion(com.huawei.hwid.a.a.a);
            this.a.setVersionName(PackageUtils.getVersionName(context));
        }
    }

    private static String a(GrsBaseInfo grsBaseInfo) {
        return "AppName: " + grsBaseInfo.getAppName() + ", AndroidVersion: " + grsBaseInfo.getVersionName() + ", IssueCountry  : " + grsBaseInfo.getIssueCountry() + ", RegCountry    : " + grsBaseInfo.getRegCountry() + ", CountrySource : " + grsBaseInfo.getCountrySource() + ", SerCountry    : " + grsBaseInfo.getSerCountry() + ", VersionName   : " + grsBaseInfo.getVersionName();
    }
}
